package com.pkche.core.advanced.form;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Map;

/**
 * 
 * <h6>封装反射操作类的属性、方法</h6>
 *
 * User: Qiang <br/>
 * Describe：利用反射方式调用类中的方法、修改类属性、遍历类结构等<br/>
 *
 * @date 2015年3月4日 上午8:30:56
 */
public class Swap extends Scatter {

	/**
	 * initialize
	 * 
	 * @param classes
	 */
	public Swap(Class<?> classes) {
		super(classes);
		// TODO Auto-generated constructor stub
	}

	/**
	 * initialize
	 * 
	 * @param typeObject
	 */
	public Swap(Object typeObject) {
		super(typeObject);
	}

	/**
	 * initialize
	 * 
	 * @param className
	 * @throws ClassNotFoundException
	 */
	public Swap(String className) throws ClassNotFoundException {
		super(Class.forName(className));
	}

	/**
	 * set the value of attribute
	 * 
	 * @param name
	 * @param value
	 */
	public void setFieldValue(String name, Object value) {
		Field field = null;
		try {
			field = typeClass.getDeclaredField(name);
			field.setAccessible(true);// set to reading
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchFieldException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			field.set(typeObject, value);
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		field.setAccessible(false);// reset
	}

	/**
	 * for the value of the attribute
	 * 
	 * @param name
	 * @return
	 */
	public Object getFieldValue(String name) {
		Field field = null;
		Object value = null;
		try {
			field = typeClass.getDeclaredField(name);
			field.setAccessible(true);// set to reading
		} catch (SecurityException e) {
			// System.out.println(typeClass.getName() + "." + name +
			// " access denied!");
			e.printStackTrace();
		} catch (NoSuchFieldException e) {
			// System.out.println(typeClass.getName() + "." + name +
			// " not found!");
			e.printStackTrace();
		}
		try {
			value = field.get(typeObject);
			field.setAccessible(false);// reset
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		return value;
	}

	/**
	 * 获取指定方法注解
	 * 
	 * @param <A>
	 * 
	 * @param methodName
	 * @return
	 */
	public <A extends Annotation> A getAnnotation(String methodName,
			Class<A> classes) {
		Method method = super.getMethod(methodName);
		if (method == null)
			return null;
		return method.getAnnotation(classes);
	}

	/**
	 * 获取指定方法注解
	 * 
	 * @param <A>
	 * @param methodName
	 * @param classes
	 * @return
	 */
	public <A extends Annotation> A getAnnotation(Method method,
			Class<A> classes) {
		return method.getAnnotation(classes);
	}

	/**
	 * 获取嵌套的值
	 * 
	 * @param <T>
	 * @param <E>
	 * @param map
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static <T> T getValue(Object element, String keys) {
		if (element == null) {
			return null;
		}
		String[] args = keys.split("\\.");
		if (element instanceof Map) {
			// 可能是Map
			Map<String, Object> map = (Map<String, Object>) element;
			int length = args.length;
			int i = 0;
			Object temp = null;
			while (length > 0) {
				String key = args[i];
				temp = map.get(key);
				if ((temp != null) && (temp instanceof Map)) {
					map = (Map<String, Object>) temp;
				}
				length--;
				i++;
			}
			return (T) temp;
		} else {
			int length = args.length;
			int i = 0;
			Object temp = null;
			while (length > 0) {
				String key = args[i];
				try {
					temp = element.getClass().getField(key);
				} catch (SecurityException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (NoSuchFieldException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				if (temp != null) {
					try {
						temp = temp.getClass().getField(key);
					} catch (SecurityException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (NoSuchFieldException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				length--;
				i++;
			}
			return (T) temp;
		}
	}

	/**
	 * 获取嵌套的值
	 * 
	 * @param <T>
	 * @param <E>
	 * @param element
	 * @param keys
	 * @param defaultValue
	 *            如果为 null默认的值
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static <T, E> T getValue(Object element, String keys,
			Object defaultValue) {
		T value = getValue(element, keys);
		if (value == null) {
			return (T) defaultValue;
		}
		return value;
	}
}
